package com.example.aly;

import com.alibaba.fastjson.JSON;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Base64;

@Component
@Slf4j
public class PopPubServer {
    public static void main(String[] args) throws Exception {
        new PopPubServer().post("0xfefe6801006916");
        String a = "0xfefe";
        String b = "68" +
                "01" +
                "00";
        String c = "69";
        String d = "16";
    }

    /**
     * 发布消息
     * @param //productKey(硬件获取)
     * @param //deviceName(硬件获取)
     * @param //TopicFullName(硬件获取)
     * @param MessageContent(该参数需要自行处理)
     * @throws Exception
     */
    public void post(String MessageContent) throws Exception {
//        log.info("设备执行成功");
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.iot20180120.Client client = Sample.createClient(System.getenv("LTAI5tSSxDerZ47qyYAEyUab"), System.getenv("cNcSd2ujmEq2BTkkNiRIyh2pWZK8JI"));
//        BASE64Encoder base64Encoder = new BASE64Encoder();
//        String encode = base64Encoder.encode(jsonString.getBytes());
        MessageContent = MessageContent.replace("0x", "").replace(" ", "");
        byte[] bytes = new byte[MessageContent.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            int index = i * 2;
            int val = Integer.parseInt(MessageContent.substring(index, index + 2), 16);
            bytes[i] = (byte) val;
        }
        String s = Base64.getEncoder().encodeToString(bytes);
        System.out.println(s);
        com.aliyun.iot20180120.models.PubRequest pubRequest = new com.aliyun.iot20180120.models.PubRequest()
                //需要修改
                .setProductKey("gx0gqRsP9Nv")
                .setIotInstanceId("iot-06z00c3eun6kkb0")
                //需要修改
                .setDeviceName("dcvice1")
                .setQos(0)
                //需要修改
                .setTopicFullName("/gx0gqRsP9Nv/dcvice1/user/update_get")
                .setContentType("hex")
//                .setMessageContent("MHhmZWZlNjgwMTAwNjkxNg==");
                .setMessageContent(s);

        //String字符串转base64

        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            log.info("成功");
            client.pubWithOptions(pubRequest, runtime);
        } catch (TeaException error) {
            // 如有需要，请打印 error
            log.info(Common.assertAsString(error.message));
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            log.info(Common.assertAsString(error.message));
        }
    }
}


